<template>
<div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo-text">二手书交易平台</div>
        <div class="sub-text">用户注册</div>
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="login-form" size="large">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable>
            <template #prefix>
              <el-icon class="el-input__icon"><User/></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" clearable show-password>
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input v-model="form.confirm" placeholder="请再次确认密码" clearable show-password>
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="role">
          <el-radio-group v-model="form.role" style="width: 100%; display: flex; justify-content: space-around;">
            <el-radio label="2">读者</el-radio>
            <el-radio label="1">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="authorize" v-if="form.role==1">
          <el-input v-model="form.authorize" placeholder="请输入管理员注册码" clearable show-password>
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex; width: 100%">
            <el-input v-model="form.validCode" style="flex: 1; margin-right: 10px" placeholder="请输入验证码"></el-input>
            <ValidCode @input="createValidCode" style="width: 100px; height: 40px"/>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="register" :loading="loading">注 册</el-button>
        </el-form-item>
        <div class="login-footer">
          <el-button type="text" @click="$router.push('/login')">已有账号？前往登录</el-button>
        </div>
      </el-form>
    </div>
</div>
</template>

<script>
import request from "../utils/request";
import {ElMessage} from "element-plus";
import ValidCode from "../components/Validate";
export default {
  name: "Register",
  components:{
    ValidCode
  },
  data(){
    return{
      loading: false,
      form:{},
      validCode: '',
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
          {
            min: 2,
            max: 13,
            message: '长度要求为2到13位',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          }
        ],
      confirm:[
        {
          required:true,
          message:"请确认密码",
          trigger:"blur"
        }
      ],
        authorize:[
          {
            required:true,
            message:"请输入注册码",
            trigger:"blur"
          }
        ],
      }
    }
    },

  methods:{
    createValidCode(data){
      this.validCode =data
    },
    register(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.validCode) {
            ElMessage.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            ElMessage.error("验证码错误")
            return
          }
          if(this.form.password != this.form.confirm)
          {
            ElMessage.error("两次密码输入不一致")
            return
          }
          if(this.form.role == 1 && this.form.authorize != "1234")
          {
            ElMessage.error("请输入正确的注册码")
            return
          }
          this.loading = true;
          request.post("user/register",this.form).then(res=>{
            this.loading = false;
            if(res.code == 0)
            {
              ElMessage.success("注册成功")
              this.$router.push("/login")
            }
            else {ElMessage.error(res.msg)}
          }).catch(() => {
            this.loading = false;
          })
        }
      })

    }
  }

  }

</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 400px;
  background: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: var(--color-primary);
  margin-bottom: 10px;
}

.sub-text {
  color: var(--color-text-secondary);
  font-size: 14px;
}

.login-footer {
  text-align: center;
  margin-top: 10px;
}
</style>